package org.algorithm;


import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.util.Arrays;
import java.util.Base64;

public class AES {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static void main(String[] args) throws Exception {

        byte[] randBytes = Base64.getDecoder().decode("AbLY5vQt");
        System.out.println( ByteBuffer.wrap(Arrays.copyOf(randBytes,4)).getInt());



        String AES_CODER_USERNAME = "^[a-zA-Z0-9\\-]+\\|[aer]=(^[A-Za-z0-9+/]+={0,2}),[aer]=(^[A-Za-z0-9+/]+={0,2}),[aer]=(^[A-Za-z0-9+/]+={0,2})$";

        boolean ismatch="00ec6240-bbc0-11ed-afbc-fbc21ce631af|a=1,e=1,r=86234568".matches(AES_CODER_USERNAME);

        System.out.println(ismatch);

        byte[] b = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

        System.out.println(Base64.getEncoder().encode(b));

        String password = "939A37AD7174E646481694602898";
        byte[] passwordByte = Arrays.copyOf(password.getBytes(StandardCharsets.UTF_8), 32);
        byte[] iv = "00092150-5225-11".getBytes(StandardCharsets.UTF_8);
        // 2. 加密字符串
        String originalText = "{\"cmd\":310,\"sn\":78,\"devType\":69001,\"ts\":1704951976,\"mac\":\"e89f6ddfcc6c\",\"data\":\"WIKKTobJ8vuCCXkIRy/TsvDfi+fb6e+95n5G2CQamj9sPmieD2KHjQszV8xW+Q2A5eXX66QSmap4lb1InuIznA==\"}";
        String encryptedText = new
                String(encrypt(originalText.getBytes(StandardCharsets.UTF_8), passwordByte, iv));
        System.out.println("Encrypted Text: " + encryptedText);
        // 3. 解密字符串
        String decryptedText = new
                String(decrypt(encryptedText.getBytes(StandardCharsets.UTF_8), passwordByte, iv));
        System.out.println("Decrypted Text: " + decryptedText);
//        INqz4PIrwGhE0oGw6EiUUaCMFTx94FpzS6TE5VmQkvKnnAs4IuGyO2qfRqQBLC3cEWxzUfuLIZwn9xaU6NIDalSd0SbPf5uRCXscUT+wxCRJpk3f3zsCPPKIzo/votNPw6topppOy+tsNO3PzhKgodR70Q/J0qVezvf2FK2nl5G1Ljd13lvXdMtcjzbvijNhjWgu6uwoRNyyVE0BobDU/Q==
//        3a730703c5eb2ca131d4
//        00ec6240-bbc0-11
    }

    public static SecretKey generateKeyFromString(byte[] keyBytes)  {
        return new SecretKeySpec(keyBytes, "AES");
    }

    // 使用AES算法加密
    public static byte[] encrypt(byte[] dataBytes, byte[] password, byte[] ivBytes) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE, generateKeyFromString(password), iv);
        byte[] encryptedBytes = cipher.doFinal(dataBytes);
        return Base64.getEncoder().encode(encryptedBytes);
    }

    // 使用AES算法解密
    public static byte[] decrypt(byte[] base64Bytes, byte[] password, byte[] ivBytes) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, generateKeyFromString(password), iv);
        byte[] encryptedBytes = Base64.getDecoder().decode(base64Bytes);
        return cipher.doFinal(encryptedBytes);
    }
}
